﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Display;
using PlanningGIS.Engine.Controls;
using PlanningGIS.ArcGIS;
using PlanningGIS.Win.Controls;
using PlanningGIS.Framework.Rule;
using PlanningGIS.Util.Convertor;
using PlanningGIS.Framework;
using PlanningGIS.Util.Data;

namespace PlanningGIS.Framework.Controls
{
    /// <summary>
    /// 作用：选择测区范围控件
    /// 作者：
    /// 修改日期：2009-06-19
    /// </summary>
    public partial class SelectFWX : UserControl
    {
        /// <summary>
        /// 用户的权限范围传递进来，通过控件控制用户角色
        /// </summary>
        private IPolygon m_pPolyUserPrivilege;  
        /// <summary>
        /// 
        /// </summary>
        private IPolygon m_pPolygon;
        /// <summary>
        /// 
        /// </summary>
        public IPolygon PolygonFWX
        {
            get
            {
                return m_pPolygon;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private IPolyline m_pPolyline;
        /// <summary>
        /// 
        /// </summary>
        public IPolyline PolylineFWX
        {
            get
            {
                return m_pPolyline;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public IPolygon PolyUserPrivilege
        {
            set
            {
                m_pPolyUserPrivilege = value;
            }
        }

        private bool showRectangle = true;
        private bool showPolygon = true;
        private bool showCircle = true;

        public bool ShowRectangle
        {
            set { showRectangle = value; }
        }

        public bool ShowPolygon
        {
            set { showPolygon = value; }
        }

        public bool ShowCircle
        {
            set { showCircle = value; }
        }

        private OpenDataDialog m_frmAddData1;

        private List<esriGeometryType> geometryTypes;

        private IHookHelper m_HookHelper;
        public IHookHelper HookHelper
        {
            set
            {
                m_HookHelper = value;
                selectAreaControl1.HookHelper = value;
            }
        }

        public void AddGeometryType(esriGeometryType geometryType)
        {
            if (geometryTypes == null)
                geometryTypes = new List<esriGeometryType>();
            geometryTypes.Add(geometryType);
        }

        public string LoadBtnText
        {
            set { this.btnLoad.Text = value; }
        }
       
        public SelectFWX()
        {
            InitializeComponent();

            selectAreaControl1.ShowImportTxt = false;
            selectAreaControl1.ShowExportTxt = false;

            selectAreaControl1.ShowRectangle = showRectangle;
            selectAreaControl1.ShowPolygon = showPolygon;
            selectAreaControl1.ShowCircle = showCircle;

            geometryTypes = new List<esriGeometryType>();
            //默认就可以加载面层图层
            geometryTypes.Add(esriGeometryType.esriGeometryPolygon);
            this.selectAreaControl1.afterButtonOkShift1 += new PlanningGIS.Framework.AfterButtonOkShift(AfterButtonOkEvent);
            this.selectAreaControl1.returnFeature += new PlanningGIS.Framework.ReturnFeatureClass(ReturnFeatureClassEvent);
        }

        public SelectFWX(bool showRectangle, bool showPolygon, bool showCircle)
        {
            InitializeComponent();

            selectAreaControl1.ShowImportTxt = false;
            selectAreaControl1.ShowExportTxt = false;

            selectAreaControl1.ShowRectangle = showRectangle;
            selectAreaControl1.ShowPolygon = showPolygon;
            selectAreaControl1.ShowCircle = showCircle;

            geometryTypes = new List<esriGeometryType>();
            //默认就可以加载面层图层
            geometryTypes.Add(esriGeometryType.esriGeometryPolygon);
            this.selectAreaControl1.afterButtonOkShift1 += new PlanningGIS.Framework.AfterButtonOkShift(AfterButtonOkEvent);
            this.selectAreaControl1.returnFeature += new PlanningGIS.Framework.ReturnFeatureClass(ReturnFeatureClassEvent);
        }

        private void ReturnFeatureClassEvent(IFeatureClass pFeatureClass)
        {
            try
            {
                IFeatureLayer pFeatureLayer = new FeatureLayer();
                pFeatureLayer.FeatureClass = pFeatureClass;
                this.axMapControl1.ClearLayers();
                this.axMapControl1.AddLayer(pFeatureLayer);
            }
            catch (System.Exception)
            {
            }
        }

        public void AfterButtonOkEvent(IGeometry pGeom, int shift)
        {
            try
            {
                if (pGeom == null || pGeom.IsEmpty || pGeom is IPoint)
                {
                    return;
                }
                if (pGeom is IEnvelope)
                {
                    pGeom = ((IEnvelope)pGeom).ToPolygon();
                }                
                m_pPolygon = pGeom as IPolygon;
                GeometryUserDraw(m_pPolygon);
            }
            catch (System.Exception)
            {
            }
            finally
            {
                Control ctl = this.Parent;
                while (ctl != null)
                {
                    if (ctl is Form)
                    {
                        Form frm = ctl as Form;
                        frm.WindowState = FormWindowState.Normal;
                        break;
                    }
                    ctl = ctl.Parent;
                }
            }
            // IActiveView actview= this.axMapControl1.ActiveView.FocusMap as IActiveView;
            // actview.Refresh();
            //this.axMapControl1.ActiveView.ScreenDisplay.UpdateWindow();
            //this.axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics| esriViewDrawPhase.esriViewGeography, null, this.axMapControl1.ActiveView.Extent);
        }

        private void GeometryUserDraw(IPolygon pPolyUserDraw)
        {
            try
            {
                if (pPolyUserDraw == null || pPolyUserDraw.IsEmpty) return; ;
                IPolyline pPolyline = pPolyUserDraw.ToPolyline();
                IRgbColor pRgbColor = new RgbColor();
                pRgbColor.Red = 255;
                pRgbColor.Green = 155;
                pRgbColor.Blue = 0;
                this.axMapControl1.Extent = this.m_HookHelper.ActiveView.Extent;
                IActiveView pActiveView = this.axMapControl1.ActiveView;
                IGraphicsContainer pGeoContainer = pActiveView as IGraphicsContainer;

                try
                {
                    pGeoContainer.DeleteAllElements();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                ISimpleLineSymbol pLineSymbol = new SimpleLineSymbol();
                ILineElement pLineElement = new LineElement() as ILineElement;
                pLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
                pLineSymbol.Color = pRgbColor;
                pLineSymbol.Width = 3;
                pLineElement.Symbol = pLineSymbol;
                IElement m_pElementUserDraw = pLineElement as IElement;
                m_pElementUserDraw.Geometry = pPolyline;
                pGeoContainer.AddElement(m_pElementUserDraw, 0);
                pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, pActiveView.Extent);
            }
            catch
            {

            }


            //if (pPolyUserDraw == null || pPolyUserDraw.IsEmpty) return; ;
            //IPolyline pPolyline = GeometryHelper.PolylineByPolygon(pPolyUserDraw);

            //IRgbColor pRgbColor = new RgbColor();
            //pRgbColor.Red = 255;
            //pRgbColor.Green = 155;
            //pRgbColor.Blue = 0;

            //IActiveView pActiveView = (m_Application.Document as IMxDocument).ActiveView;
            //IGraphicsContainer pGeoContainer = pActiveView as IGraphicsContainer;
            //if (m_pElementUserDraw != null)
            //    try
            //    {
            //        pGeoContainer.DeleteElement(m_pElementUserDraw);
            //    }
            //    catch (Exception ex)
            //    {
            //        Console.WriteLine(ex.Message);
            //    }

            //ISimpleLineSymbol pLineSymbol = new SimpleLineSymbolClass();
            //ILineElement pLineElement = new LineElementClass();
            //pLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
            //pLineSymbol.Color = pRgbColor;
            //pLineSymbol.Width = 3;
            //pLineElement.Symbol = pLineSymbol;
            //m_pElementUserDraw = pLineElement as IElement;
            //m_pElementUserDraw.Geometry = pPolyline;
            //pGeoContainer.AddElement(m_pElementUserDraw, 0);
            //pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, pActiveView.Extent);
        }

        /// <summary>
        /// 选择测区图层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdAddDataOk_Click(object sender, EventArgs e)
        {
            List<IDataset> allDatasets = m_frmAddData1.GetAllDatasets();// m_frmAddData1.GetAllDatasets();

            if (allDatasets.Count <= 0)
                return;

            IDataset dataset = allDatasets[0];
            if (dataset == null)
            {
                MsgHelper.ShowInfomationMsgbox("请选择正确的数据！");
                m_frmAddData1.Hide();
                return;
            }

            if (dataset.Type == esriDatasetType.esriDTFeatureDataset ||
                dataset.Type == esriDatasetType.esriDTRasterCatalog ||
                dataset.Type == esriDatasetType.esriDTRasterDataset)
            {
                MsgHelper.ShowInfomationMsgbox("请选择要素类！");
                m_frmAddData1.Hide();
                return;
            }

            IFeatureClass pFeatureClass = dataset as IFeatureClass;
            IFeatureLayer pFeatureLayer = null;
            if (geometryTypes.Contains(pFeatureClass.ShapeType))
            {
                int iCount = pFeatureClass.FeatureCount(null);
                if (iCount == 1)
                {
                    pFeatureLayer = new FeatureLayer();
                    pFeatureLayer.Name = pFeatureClass.AliasName;
                    pFeatureLayer.FeatureClass = pFeatureClass;
                }
                else
                {
                    MsgHelper.ShowInfomationMsgbox("所选数据中必须是仅有一个要素！");
                    m_frmAddData1.Hide();
                    return;
                }
            }
            else
            {
                MsgHelper.ShowInfomationMsgbox("所选数据的几何图形不符合要求！");
                m_frmAddData1.Hide();
                return;
            }

            if (pFeatureLayer != null)
            {
                //测区范围面预览
                this.axMapControl1.ClearLayers();
                this.axMapControl1.AddLayer((ILayer)pFeatureLayer);
                this.axMapControl1.get_Layer(0).Visible = true;

                //获取测区范围面
                IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
                IFeature pFeature = pFeatureCursor.NextFeature();

                switch (pFeatureClass.ShapeType)
                {
                    case esriGeometryType.esriGeometryPolyline:
                        m_pPolyline = (IPolyline)pFeature.ShapeCopy;
                        break;
                    case esriGeometryType.esriGeometryPolygon:
                        m_pPolygon = (IPolygon)pFeature.ShapeCopy;
                        ITopologicalOperator2 pTop = m_pPolygon as ITopologicalOperator2;
                        pTop.IsKnownSimple_2 = false;
                        pTop.Simplify();
                        m_pPolyline = pTop.Boundary as IPolyline;
                        break;
                }
            }
            else
            {
                MsgHelper.ShowInfomationMsgbox("测区范围面层必须是仅有一个要素的单链要素层");
                m_frmAddData1.Hide();
                return;
            }

            m_frmAddData1.Hide();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (m_frmAddData1 == null)
                m_frmAddData1 = new OpenDataDialog();

            m_frmAddData1.MultiSelect = false;
            m_frmAddData1.OnOkClick = new EventHandler(CmdAddDataOk_Click);
            m_frmAddData1.ShowDialog(this);
        }
    }
}
